#include "stdafx.h"

#include "MvCamera.h"

namespace Dahua{

CMvCamera::CMvCamera(const GenICam::ICameraPtr& sptrCamera)
	: m_sptrCamera(sptrCamera)
	, m_pMvCameraSink(NULL)
{

}

CMvCamera::~CMvCamera()
{

}

bool CMvCamera::open(IMvCameraSink* pSink)
{
	if ( NULL == m_sptrCamera )
	{
		return false;
	}

	bool bConnRet = m_sptrCamera->connect();
	if ( !bConnRet )
	{
		return false;
	}

	GenICam::IEventSubscribePtr sptrEventSubscribe = GenICam::CSystem::getInstance().createEventSubscribe(m_sptrCamera);
	if ( NULL != sptrEventSubscribe &&
		NULL != pSink)
	{
		sptrEventSubscribe->subscribeConnectArgs(GenICam::ConnectArgProc(&CMvCamera::procConnectArg, this));
		m_pMvCameraSink = pSink;
	}

	return true;
}

void CMvCamera::close()
{
	if ( NULL != m_sptrCamera )
	{
		m_sptrGigECamera.reset();
		m_sptrGigEInterface.reset();
		m_sptrParamCtrlSet.reset();
		m_sptrDisplayHelper.reset();
		m_pMvCameraSink = NULL;

		m_sptrCamera->disConnect();
	}
}

std::string CMvCamera::getDevUserID()
{
	if ( NULL == m_sptrCamera )
	{
		return "Too lazy to set a ID";
	}

	return m_sptrCamera->getName();
}

bool CMvCamera::forceIpAddress( const char* pIpAddr, const char* pMaskAddr, const char* pGatewayAddr )
{
	if ( NULL == m_sptrGigECamera )
	{
		m_sptrGigECamera = GenICam::IGigECamera::getInstance(m_sptrCamera);
	}

	return m_sptrGigECamera->forceIpAddress(pIpAddr, pMaskAddr, pGatewayAddr);
}

bool CMvCamera::startDisplay(void* pShowHandle, int iShowRate)
{
	if ( !m_sptrCamera->isConnected() )
	{
		return false;
	}

	if ( NULL == m_sptrDisplayHelper )
	{
		m_sptrDisplayHelper = CMvDisplayHelperPtr(new CMvDisplayHelper(m_sptrCamera));
	}

	if ( !m_sptrDisplayHelper->isValid() )
	{
		return false;
	}

	return m_sptrDisplayHelper->startDisplay(pShowHandle, iShowRate);
}

bool CMvCamera::stopDisplay()
{
	if ( NULL == m_sptrDisplayHelper )
	{
		return false;
	}

	return m_sptrDisplayHelper->stopDisplay();
}

bool CMvCamera::subscribeParamUpdate(const ParamVectorType* pParamList)
{
	// TODO
	return false;
}

CMvCamera::ParamControlSetPtr CMvCamera::getParamControl()
{
	if ( NULL == m_sptrParamCtrlSet &&
		m_sptrCamera->isConnected() )
	{
		m_sptrParamCtrlSet = ParamControlSetPtr(new ParamControlSet());
		m_sptrParamCtrlSet->_sptrAcquistionCtrl = GenICam::CSystem::getInstance().createAcquisitionControl(m_sptrCamera);
		m_sptrParamCtrlSet->_sptrAnalogCtrl = GenICam::CSystem::getInstance().createAnalogControl(m_sptrCamera);
		m_sptrParamCtrlSet->_sptrImageFormatCtrl = GenICam::CSystem::getInstance().createImageFormatControl(m_sptrCamera);
		m_sptrParamCtrlSet->_sptrIspCtrl = GenICam::CSystem::getInstance().createISPControl(m_sptrCamera);
		m_sptrParamCtrlSet->_sptrUserSetCtrl = GenICam::CSystem::getInstance().createUserSetControl(m_sptrCamera);
	}

	return m_sptrParamCtrlSet;
}
// 获取Gige 相机信息
// get gige camera information
CMvCamera::NetAddressInfoPtr CMvCamera::getGigECameraInfo()
{
	if ( NULL == m_sptrGigECamera )
	{
		m_sptrGigECamera = GenICam::IGigECamera::getInstance(m_sptrCamera);
	}

	if ( NULL == m_sptrGigECamera )
	{
		return CMvCamera::NetAddressInfoPtr();
	}

	CMvCamera::NetAddressInfoPtr sptrCameraIpInfo(new CMvCamera::NetAddressInfo());
	sptrCameraIpInfo->_strGatewayAddr = m_sptrGigECamera->getGateway().c_str();
	sptrCameraIpInfo->_strMaskAddr = m_sptrGigECamera->getSubnetMask().c_str();
	sptrCameraIpInfo->_strIpAddr = m_sptrGigECamera->getIpAddress().c_str();
	sptrCameraIpInfo->_strMac = m_sptrGigECamera->getMacAddress().c_str();

	return sptrCameraIpInfo;
}

// 获取Gige 接口信息
// get gige camera interface information
CMvCamera::NetAddressInfoPtr CMvCamera::getGigEInterfaceInfo()
{
	if ( NULL == m_sptrGigEInterface )
	{
		m_sptrGigEInterface = GenICam::IGigEInterface::getInstance(m_sptrCamera);
	}

	if ( NULL == m_sptrGigEInterface )
	{
		return CMvCamera::NetAddressInfoPtr();
	}

	CMvCamera::NetAddressInfoPtr sptrInterfaceInfo(new CMvCamera::NetAddressInfo());
	sptrInterfaceInfo->_strGatewayAddr = m_sptrGigEInterface->getGateway().c_str();
	sptrInterfaceInfo->_strMaskAddr = m_sptrGigEInterface->getSubnetMask().c_str();
	sptrInterfaceInfo->_strIpAddr = m_sptrGigEInterface->getIpAddress().c_str();
	sptrInterfaceInfo->_strMac = m_sptrGigEInterface->getMacAddress().c_str();

	return sptrInterfaceInfo;
}

void CMvCamera::enableRateCtrl(bool bEnable)
{
	if ( NULL != m_sptrDisplayHelper )
	{
		m_sptrDisplayHelper->enableRateCtrl(bEnable);
	}
}

void CMvCamera::procConnectArg(const GenICam::SConnectArg& arg)
{
	if ( GenICam::SConnectArg::offLine == arg.m_event &&
		NULL != m_pMvCameraSink)
	{
		m_pMvCameraSink->onCameraDisconnect();
	}
}

} // namespace Dahua
